package com.luluteam.pushlib.tools;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.luluteam.pushlib.R;
import com.luluteam.pushlib.model.PushMessage;
import com.luluteam.pushlib.util.StringUtils;

/**
 * @author Guan
 * @date Created on 2018/12/7
 */
public class NotificationHelper {

    private static String TAG = "NotificationHelper";
    private static int index = 1;


    public static int getDrawableId(Context paramContext, String paramString) {
        return paramContext.getResources().getIdentifier(paramString,
                "drawable", paramContext.getPackageName());
    }


    /**
     * 弹出 Notification 消息
     *
     * @param pushMessage
     */
    public static void sendNotification(Context context, PushMessage pushMessage) {

//        sendPushMessageReceive(pushMessage);

        //Click
        Intent intentClick = new Intent(context, NotificationBroadcastReceiver.class);
        intentClick.setAction("notification_clicked");

        if (StringUtils.isNotEmpty(pushMessage.getTargetActivity())) {
            Log.i(TAG, " TargetActivity 为" + pushMessage.getTargetActivity());
            intentClick.putExtra("targetActivity", pushMessage.getTargetActivity());
        }

        if (pushMessage.getTargetParams() != null) {
            Log.i(TAG, " getTargetParams 为" + pushMessage.getTargetParams().toString());
            intentClick.putParcelableArrayListExtra("targetParams", pushMessage.getTargetParams());
        }
        intentClick.putExtra("messageId", pushMessage.getMessageId());
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(context, index++, intentClick, PendingIntent.FLAG_UPDATE_CURRENT);


        //Cancle
        Intent intentCancel = new Intent(context, NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra("messageId", pushMessage.getMessageId());
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, index++, intentCancel, PendingIntent.FLAG_UPDATE_CURRENT);

        //获取NotificationManager实例
        NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //实例化NotificationCompat.Builde并设置相关属性
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                //设置通知标题
                .setContentTitle(pushMessage.getTitle())
                //设置推送内容
                .setContentText(pushMessage.getContent())
                //设置打开动作
                // TODO BUG android6.0 的设备点击后出现闪退
                .setContentIntent(pendingIntentClick)
                //设置取消动作
                .setDeleteIntent(pendingIntentCancel)
                //允许用户滑动取消
                .setAutoCancel(true);


        //设置图标会出现Bug
//        if (!pushMessage.getIcon().equals("")) {
//            int id = getDrawableId(context, pushMessage.getIcon());
//            System.out.println("icon id======" + id);
//            builder.setSmallIcon(R.drawable.avatar);
//        }
//        if (!pushMessage.getLargeIcon().equals("")) {
//            System.out.println("getLargeIcon......");
//            Bitmap largeBitmap = BitmapFactory.decodeResource(context.getResources(), getDrawableId(context, pushMessage.getLargeIcon()));
//            System.out.println("largeBitmap==" + largeBitmap.toString());
//            builder.setLargeIcon(largeBitmap);
//        }

        int flag = 0;
        if (pushMessage.getVibrate().equals("true")) {
            flag = Notification.DEFAULT_VIBRATE;
        }
        if (pushMessage.getSound().equals("true")) {
            flag |= Notification.DEFAULT_SOUND;
        }
        if (pushMessage.getFlash().equals("true")) {
            flag |= Notification.DEFAULT_LIGHTS;
        }
        builder.setDefaults(flag);

        Notification notification = builder.build();

        System.out.println("pushMessage==" + pushMessage.toString());
        notifyManager.notify(index++, notification);

    }

}
